var express = require('express');
var router = express.Router();
var utils = require('../utils');
var sql = require('../sql');
var Address = require('../sql/col/Address');
var Order = require('../sql/col/Order');
var uuid = require('node-uuid');


router.get('/', function(req,res, next) {
    res.send('address相关接口')
});

/**
 * @api {post} /api/address/add 添加地址
 * @apiName PostAddressAdd
 * @apiGroup address
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { string } addressDetail 详细地址 
 * @apiParam { String } province 省
 * @apiParam { string } city 城市 
 * @apiParam { string } county 区/县 
 * @apiParam { String } areaCode 地区编码
 * @apiParam { Boolean } isDefault 默认地址
 * @apiParam { String } name 姓名
 * @apiParam { String } tel 电话
 * @apiParam { string } token
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
      code: '200',
      message: '插入地址成功'
    })

 * @apiSampleRequest /api/address/add
 * @apiVersion 0.0.0
 */
router.post('/add', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const insertData = req.body
        insertData.addressid = 'address_' + uuid.v1()
        insertData.time = new Date().getTime()
        if (insertData.isDefault) {
            const userid = insertData.userid;
            sql.update(Address, { userid }, { $set: { isDefault: false}}, 1).then(() => {
                sql.insert(Address, insertData).then(() => {
                    res.send({
                        code: '200',
                        messsage: '保存地址成功'
                    })
                })
            })
        } else {
            sql.find(Address, { userid: insertData.userid }, { _id:0}).then(data => {
                if (data.length === 0) {
                    insertData.isDefault = true
                }
                sql.insert(Address, insertData).then(() => {
                    res.send({
                        code: '200',
                        messsage: '保存地址成功'
                    })
                })
            })
            
        }
    })
})

/**
 * @api {post} /api/address/updateOrderAddress 更改订单的地址
 * @apiName PostUpdateOrderAddress
 * @apiGroup address
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { string } time 下单时间
 * @apiParam { String } name 姓名
 * @apiParam { String } tel 电话
 * @apiParam { String } address 完整地址
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
      code: '200',
      message: '更改订单的地址'
    })

 * @apiSampleRequest /api/address/updateOrderAddress
 * @apiVersion 0.0.0
 */
router.post('/updateOrderAddress', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid, time, name, tel, address } = req.body
        sql.update(Order, { userid, time }, { $set: { name, tel, address}}, 1).then(()=> {
            res.send({
                code: '200',
                message: '更新订单地址成功'
            })
        })
    })
})

/**
 * @api {post} /api/address/defaultAddress 获取默认地址
 * @apiName PostDefaultAddress
 * @apiGroup address
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
      code: '200',
      message: '获取默认地址成功',
      data
    })

 * @apiSampleRequest /api/address/defaultAddress
 * @apiVersion 0.0.0
 */
router.post('/defaultAddress', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid } = req.body
        sql.find(Address, { userid, isDefault: true }, { _id:0 }).then(data => {
            res.send({
                code: '200',
                message: '获取默认地址成功',
                data
            })
        })
    })
})

/**
 * @api {post} /api/address/addresslist 获取地址列表
 * @apiName PostAddressList
 * @apiGroup address
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '获取地址列表成功',
        data
    })

 * @apiSampleRequest /api/address/addresslist
 * @apiVersion 0.0.0
 */
router.post('/addresslist', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid } = req.body
        sql.sort(Address, { userid }, { _id:0 }, { time: -1}).then(data => {
            res.send({
                code: '200',
                message: '获取地址列表成功',
                data
            })
        })
    })
})

/**
 * @api {post} /api/address/singleAddress 获取单条地址
 * @apiName PostAddressSingleAddress
 * @apiGroup address
 * 
 * @apiParam { string } addressid 地址编号
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *   res.send({
        code: '200',
        message: '获取单条地址信息成功',
        data
    })

 * @apiSampleRequest /api/address/singleAddress
 * @apiVersion 0.0.0
 */
router.post('/singleAddress', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { addressid } = req.body
        sql.find(Address, { addressid }, { _id:0 }).then(data => {
            res.send({
                code: '200',
                message: '获取单条地址信息成功',
                data
            })
        })
    })
})

/**
 * @api {post} /api/address/updateSingleAddress 修改单条地址
 * @apiName PostAddressUpdateSingleAddress
 * @apiGroup address
 * 
 * @apiParam { string } userid 
 * @apiParam { string } addressid 地址编号
 * @apiParam { String } name 姓名
 * @apiParam { String } tel 电话
 * @apiParam { String } province 省
 * @apiParam { string } city 城市 
 * @apiParam { string } county 区/县 
 * @apiParam { string } areaCode 地区编码 
 * @apiParam { string } addressDetail 详细地址 
 * @apiParam { Boolean } isDefault 默认地址
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '修改单条地址信息成功'
    })

 * @apiSampleRequest /api/address/updateSingleAddress
 * @apiVersion 0.0.0
 */
router.post('/updateSingleAddress', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid, addressid, name, tel, province, city, county, areaCode, addressDetail, isDefault } = req.body
        const time = new Date().getTime() // 地址修改时间
        if (isDefault) {
            sql.update(Address, { userid }, { $set: { isDefault: false} }, 1)
        } 
        sql.update(Address,{ addressid }, { $set: { name, tel, province, city, county, areaCode, addressDetail, isDefault, time }}).then(() => {
            res.send({
                code: '200',
                message: '修改单条地址信息成功'
            })
        })
    })
})

/**
 * @api {post} /api/address/deleteSingleAddress 删除单条地址
 * @apiName PostAddressDeleteSingleAddress
 * @apiGroup address
 * 
 * @apiParam { string } addressid 地址编号
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '删除单条地址信息成功',
    })

 * @apiSampleRequest /api/address/deleteSingleAddress
 * @apiVersion 0.0.0
 */
router.post('/deleteSingleAddress', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { addressid } = req.body
        sql.delete(Address, { addressid }, { _id:0 }).then(() => {
            res.send({
                code: '200',
                message: '删除单条地址信息成功',
            })
        })
    })
})

module.exports = router;
